<?php

namespace app\model\iot_model;

use app\BaseModel;
use app\common\Message;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

class IotRoom extends BaseModel
{
    use Message;
    protected $name = 'iot_room';

    /**
     * 功能室名称
     * @return BelongsTo
     */
    public function iotFunctionRoom(): BelongsTo
    {
        return $this->belongsTo(IotFunctionRoom::class,'name_id')->bind([
            'function_room_name'=>'name',
            'function_room_img' => 'img'
        ]);
    }

    /**
     * 功能室设备
     * @return HasMany
     */
    public function iotDevice(): HasMany
    {
        return $this->hasMany(IotDevice::class, 'room_id');
    }

    /**
     * 功能室预约记录
     * @return HasMany
     */
    public function roomReservation(): HasMany
    {
        return $this->hasMany(IotRoomReservation::class, 'room_id');
    }

    /**
     * 关联功能室分组-获取名称
     * @return BelongsTo
     */
    public function groupingForName(): BelongsTo
    {
        return $this->belongsTo(IotGrouping::class, 'type_id')->bind(['name']);
    }

    /**
     * 关联功能室设置表
     * @return BelongsTo
     */
    public function functionRoomForName(): BelongsTo
    {
        return $this->belongsTo(IotFunctionRoom::class, 'name_id');
    }

    /**
     * 关联功能室设置-获取功能室名称
     * @return BelongsTo
     */
    public function functionRoomName(): BelongsTo
    {
        return $this->belongsTo(IotFunctionRoom::class, 'name_id')->bind(['function_room_name'=>'name']);
    }

    /**
     * 关联设备表-获取设备ID
     * @return HasMany
     */
    public function getDeviceList(): HasMany
    {
        return $this->hasMany(IotDevice::class,'room_id')->field('id,room_id');
    }
}